let text = require('../../../../common/util/text.js')
const app = getApp()
Page({
    data: {
        video: {},
        play: true, // true : 播放 ｜ false : 暂停
        max: '',
        min: 0,
        value: 0,
        treatment_item_id:'',
        day:'',
        show:false,
        additions:false,
        page_show:false,
        showtips:false,
        type:'',
        list:[{
          item:[{ cn: '治疗的类型、名称', key: 'treatment_name', value: '', type: 'text' , range: [], }, 
          { cn: '主动还是被动', key: 'is_active', value: '', type: 'picker', range: ["主动", "被动"], }, 
          { cn: '治疗是否方便', key: 'is_convenient', value: '', type: 'picker', range: ["方便", "不方便"] }, 
          { cn: '贵还是便宜', key: 'is_expensive', value: '', type: 'picker', range: ["便宜", "昂贵"]},
          ]
        }],
        addlist:[{ cn: '治疗的类型、名称', key: 'treatment_name', value: '', type: 'text' , range:[] }, 
        { cn: '主动还是被动', key: 'is_active', value: '', type: 'picker', range: ["主动", "被动"], }, 
        { cn: '治疗是否方便', key: 'is_convenient', value: '', type: 'picker', range: ["方便", "不方便"] }, 
        { cn: '贵还是便宜', key: 'is_expensive', value: '', type: 'picker', range: ["便宜", "昂贵"]},
        ],
        reatment:[],
        is_edit:false,
        range:[],
        is_add:true,
        update_id:'',
        current:-1,
        PracticeStatus:false,
        is_write:false 
    },
    onLoad: function (opt) {
        let that = this
        that.setData({
          treatment_item_id:opt.treatment_item_id?opt.treatment_item_id:'',
          week:opt.week?Number(opt.week):'',
          type:opt.type
        })
        that.xGetHelp()
        that.xToPracticeStatus()
    },
    xChildReload: function () {
      var pages = getCurrentPages();//当前页面栈
      if (pages.length > 1) {
        var beforePage = pages[pages.length - 2];//获取上一个页面实例对象
        beforePage.xInfo();//触发父页面中的方法
      }
    },
    bindloadedmetadata:function(e){
      this.setData({
        value:e.detail.duration
      })
    },
    bindpause(e){
      console.log('暂停播放--'+JSON.stringify(e))
    },
    onShow: function () {
        let that = this 
        try {
          var value = wx.getStorageSync('treatmentlist')
          if (value !='') {
            that.setData({
              list:value
            })
          }else{
            
          }
        } catch (e) {
        }
        that.xGetlist()
        getApp().globalData.lock=false

    },
    onHide: function () {
        
    },
    // 获取所有治疗方式信息
    xGetlist:function(){
      let that =this;
      wx.cloud.callFunction({
        name: 'digital_therapy_extend',
        data: {
          action: "get_other_treatment_received"
        }
      }).then(res => {
         var range = [ ]
         for(let index in res.result){
            range.push(res.result[index].treatment_name)
         }
         that.setData({
            [`list[0].item[0].range`]:range,
            [`addlist[0].range`]:range,
            range:range
         })
         that.xGettreatment('init')
       })
    },
    xInput: function (e) {
      let that = this
      let value = e.detail.value
      let params = e.currentTarget.dataset
      that.setData({
          [`addlist[${params.idx}].value`]: value
      })
    },
    xPicker: function (e) {
        let that = this
        let sel = e.detail.value
        let params = e.currentTarget.dataset
        let range = that.data.addlist[params.idx].range
        let value = range[sel]
        that.setData({
            [`addlist[${params.idx}].value`]: value
        })
    },
    xSet: function (e) {
      let that = this
      let params = e.currentTarget.dataset
      that.setData({
          [`addlist[${params.idx}].value`]: params.value
      })
    },
    xSeta: function (e) {
      let that = this
      let params = e.currentTarget.dataset
      that.setData({
        [`list[${params.index}].item[${params.idx}].value`]: params.value
      })
    },
    xInputa: function (e) {
      let that = this
      let value = e.detail.value
      let params = e.currentTarget.dataset
      that.setData({
          [`list[${params.index}].item[${params.idx}].value`]: value
      })
    },
    xPickera: function (e) {
        let that = this
        let sel = e.detail.value
        let params = e.currentTarget.dataset
        let range = that.data.list[params.index].item[params.idx].range
        let value = range[sel]
        that.setData({
          [`list[${params.index}].item[${params.idx}].value`]: value
        })
    },
    // 未完成新增
    xToaddnew:function(){
        let that =this;
        let newarr ={
          item:[{ cn: '治疗的类型、名称', key: 'treatment_name', value: '', type: 'text' , range:that.data.range }, 
          { cn: '主动还是被动', key: 'is_active', value: '', type: 'picker', range: ["主动", "被动"], }, 
          { cn: '治疗是否方便', key: 'is_convenient', value: '', type: 'picker', range: ["方便", "不方便"] }, 
          { cn: '贵还是便宜', key: 'is_expensive', value: '', type: 'picker', range: ["便宜", "昂贵"]},
          ]
        }
        let obj = that.data.list
        obj.splice(that.data.list.length+1,0,newarr)
        that.setData({
          [`list[${that.data.list.length-1}]`]:newarr
        })
    },
    // 新增
    xToadd:function(){
        let that =this;
        for(let index in that.data.addlist){
           that.setData({
            [`addlist[${index}].value`]:''
           })
        }
        that.setData({
          additions:!this.data.additions,
          is_add:true,
          page_show:!this.data.additions,
          showtips:false
        })
        // wx.setStorageSync('treatmentlist', that.data.list)
    },
    // 编辑
    xToEdit:function(e){
        let that =this;
        that.setData({
          is_add:false,
          additions:true,
          showtips:false,
          page_show:true,
          current:e.currentTarget.dataset.index,
          [`addlist[0].value`]:e.currentTarget.dataset.item.treatment_name,
          [`addlist[1].value`]:e.currentTarget.dataset.item.is_active?'主动':'被动',
          [`addlist[2].value`]:e.currentTarget.dataset.item.is_convenient?'方便':'不方便',
          [`addlist[3].value`]:e.currentTarget.dataset.item.is_expensive?'便宜':'昂贵',
          update_id:e.currentTarget.dataset.item._id
        })
    },
    determine_edit:function(){
      let that =this;
      let _dataObj = [{
        _id:that.data.update_id,
        treatment_name:that.data.addlist[0].value,
        is_active:that.data.addlist[1].value == '主动'?true:false,
        is_convenient:that.data.addlist[2].value == '方便'?true:false,
        is_expensive:that.data.addlist[3].value == '便宜'?true:false,
        treatment_item_id:that.data.treatment_item_id,
        week:that.data.week
      }]
      wx.cloud.callFunction({
        name: 'digital_therapy_extend',
        data: {
          action: "update_user_perception_treatment",
          dataObj: _dataObj
        }
      }).then(res => {
        that.setData({
          additions:false,
          page_show:false
        })
        that.xGettreatment()
      })
    },
    // determine:function(){
    //     let that =this;
    //     for(let index in that.data.addlist){
    //       if(that.data.addlist[index].value ==''){
    //         wx.showModal({
    //           title: "提示",
    //           content: that.data.addlist[index].cn + '未填写',
    //           showCancel: false,
    //           confirmText:'确定'
    //         })
    //         return
    //       }
    //     }
    //     let  newarr ={
    //       item:that.data.addlist
    //     }
    //     let index  =that.data.list.length
    //     let obj = that.data.list
    //     obj.splice(that.data.list.length+1,0,newarr)
    //     that.setData({
    //       additions:false,
    //       [`list[${index}]`]:newarr
    //     })
    //     wx.setStorageSync('treatmentlist', that.data.list)
    // },
    // 新增
    determine_add:function(){
      let that =this;
      for(let index in that.data.addlist){
        if(that.data.addlist[index].value ==''){
          wx.showModal({
            title: "提示",
            content: that.data.addlist[index].cn + '未填写',
            showCancel: false,
            confirmText:'确定'
          })
          return
        }
      }
      let _dataObj = [{
        treatment_name:that.data.addlist[0].value,
        is_active:that.data.addlist[1].value == '主动'?true:false,
        is_convenient:that.data.addlist[2].value == '方便'?true:false,
        is_expensive:that.data.addlist[3].value == '便宜'?true:false,
        treatment_item_id:that.data.treatment_item_id,
        week:that.data.week
      }]
      wx.cloud.callFunction({
        name: 'digital_therapy_extend',
        data: {
          action: "add_user_perception_treatment",
          dataObj: _dataObj
        }
      }).then(res => {
        that.setData({
          additions:false,
          page_show:false
        })
        that.xGettreatment()
       })
    },
    xTodel:function(e){
        let that =this;
        let index  =e.currentTarget.dataset.index
        that.data.list.splice(index,1)
        that.setData({
          list:that.data.list
        })
    },
    onUnload:function(){
        let that =this
        that.xChildReload()
    },
    xTodeltrue:function(){
        let that =this;
        let _id =that.data.update_id
        let _dataObj = {_id:_id}
        wx.showModal({
          title: "提示",
          content:'删除后无法恢复，确定删除吗？',
          success (res) {
            if (res.confirm) {
              wx.cloud.callFunction({
                name: 'digital_therapy_extend',
                data: {
                  action: "remove_user_perception_treatment",
                  dataObj: _dataObj
                }
              }).then(res => {
                  that.setData({
                    additions:false,
                    page_show:false
                  })
                  that.xGettreatment()
              })
            } else if (res.cancel) {
              
            }
          }
        })
        
    },
    // 保存
    xToSave:function(){
        let that = this;
        for(let index in that.data.list){
          for(let i in that.data.list[index].item){
              if(that.data.list[index].item[i].value ==''){
                wx.showModal({
                  title: "提示",
                  content: that.data.list[index].item[i].cn + '未填写',
                  showCancel: false,
                  confirmText:'确定'
                })
                return
              }
          }
        }
        wx.showLoading({
          title: '保存中',
        })
        // wx.clearStorageSync('treatmentlist')
        for(var i=0;i<that.data.list.length+1;i++){
          that.xSave(i)
        }
    },
    xSave:function(index){
      let that =this;
      if(index>that.data.list.length-1){
          setTimeout(()=>{
            wx.hideLoading()
            wx.showToast({
              title: '保存成功',
              icon:'none'
            })
            that.xGettreatment(1)
          },2000)
      }else{
          let _dataObj = [{
            treatment_name: that.data.list[index].item[0].value,
            is_active:that.data.list[index].item[1].value == '主动'?true:false,
            is_convenient:that.data.list[index].item[2].value == '方便'?true:false,
            is_expensive:that.data.list[index].item[3].value == '便宜'?true:false,
            treatment_item_id:that.data.treatment_item_id,
            week:that.data.week
          }]
          wx.cloud.callFunction({
            name: 'digital_therapy_extend',
            data: {
              action: "add_user_perception_treatment",
              dataObj: _dataObj
            }
          }).then(res => {
            
          })
      }
      
    },
    // 获取清单
    xGettreatment:function(type){
        let that =this;
        wx.cloud.callFunction({
          name: 'digital_therapy_extend',
          data: {
            action: "get_user_perception_treatment"
          }
        }).then(res => {
            that.setData({
              reatment:res.result.list,
              video:res.result.video
            })
            if(res.result.list.length>0){
              that.setData({
                list:[]
              })
              var newarr=[]
              for(let index in res.result.list){
                let item ={
                  item:[{ cn: '治疗的类型、名称', key: 'treatment_name', value: res.result.list[index].treatment_name, type: 'text' , range:that.data.range, }, 
                  { cn: '主动还是被动', key: 'is_active', value: res.result.list[index].is_active?'主动':'被动', type: 'picker', range: ["主动", "被动"], }, 
                  { cn: '治疗是否方便', key: 'is_convenient', value: res.result.list[index].is_convenient?'方便':'不方便', type: 'picker', range: ["方便", "不方便"] }, 
                  { cn: '贵还是便宜', key: 'is_expensive', value: res.result.list[index].is_expensive?'便宜':'昂贵', type: 'picker', range: ["便宜", "昂贵"]},
                  ]
                } 
                newarr.push(item)
              }
              that.setData({
                list:newarr
              })
            }else{
              if(type =='init'){
                that.setData({
                  is_edit:'init'
                })
              }
            }
            if(type==1){
              that.setData({
                is_edit:false
              })
            }
         })
    },
    xToPracticeStatus:function(){
      let that =this;
      wx.cloud.callFunction({
        name: 'practice_help',
        data: {
          action: "get_practice_help_status",
          dataObj: {
            num:9
          }
        }
      }).then(res => {
        for(let index in res.result){
          if(res.result[index]._id==1){
            that.setData({
              PracticeStatus:res.result[index].status
            })
          }
        }
      })
    },
    xToSetIsWR:function(){
        this.setData({
          is_write:!this.data.is_write
        })
        wx.pageScrollTo({
          duration: 0,
          scrollTop:0
        })
    },
    xGetHelp:function(){
        let that =this;
        let _dataObj = {
          num:9,//练习任务类型数值
          type:1,//1:练习说明
        }
        wx.cloud.callFunction({
          name: 'practice_help',
          data: {
            action: "get_practice_help",
            dataObj: _dataObj
          }
        }).then(res => {
          res.result[0].content=text.text(res.result[0].content)
            this.setData({
              practice_help:res.result[0]
            })
        })
    },
    xTosetM:function(){
      this.setData({
        showtips:!this.data.showtips,
        page_show:true,
        additions:false
      })
      wx.setStorageSync(this.data.treatment_item_id, this.data.treatment_item_id)
    },
    xTosetMH:function(){
      this.setData({
        showtips:false,
        page_show:false,
        additions:false
      })
    }
})